package cn.patpang.core.vo;

import cn.patpang.config.HttpResultMessage;
import org.apache.commons.lang3.StringUtils;

/**
 * http result
 *
 * @Title HttpJsonResult.java
 * @Author Pat Pang
 * @Email pat@patpang.cn
 * @Data 2017年6月26日 下午2:17:04
 */
public class HttpResult {

    private Long code;
    private String message;
    private Object data;

    public static HttpResult error(Integer code) {
        return new HttpResult(Long.valueOf("400000" + code), HttpResult.getMessage("400000." + code), null);
    }

    public static HttpResult error(Integer code, Object data) {
        return new HttpResult(Long.valueOf("400000" + code), HttpResult.getMessage("400000." + code), data);
    }

    public static HttpResult success() {
        return HttpResult.success(null);
    }

    public static HttpResult success(Object data) {
        return new HttpResult(200000L, HttpResult.getMessage("200000"), data);
    }

    private HttpResult(Long code, String message, Object data) {
        this.code = code;
        this.message = message;
        this.data = data;
    }

    private static String getMessage(String code) {
        String resultMsg;
        resultMsg = HttpResultMessage.get("httpresult." + code);
        return StringUtils.isNotBlank(resultMsg) ? resultMsg : HttpResultMessage.get("httpresult.400000.999999");
    }

    public Long getCode() {
        return code;
    }

    public String getMessage() {
        return message;
    }

    public Object getData() {
        return data;
    }

}
